import { IdfaModel } from "src/mongodb/models/idfaModel/idfa.models";
import { QudaotjModel } from "src/mongodb/models/qudaotjModel/qudaotjModel";
import { ModelType } from "@typegoose/typegoose/lib/types";
import axios from 'axios';
import * as crypto from 'crypto'

export enum qudaoEnum {
    '无渠道' = 'null',
    '有道' = 'youdao',
    'UC' = 'uc',
    '知乎' = 'zhihu',
    '抖音' = 'douyin',
    '刷宝' = 'shubao',
    '百度' = 'baidu',
    '新浪' = 'xinlang',
    '乐游' = 'leyou',
    '搜狗' = 'shougou',
    '快手' = 'kuaishou',
    '趣头条' = 'qutoutiao',
    '聚流宝' = 'juliubao',
    '广点通' = 'guangdiantong',
    '喜马拉雅' = 'xmly',
    '东方头条' = 'dongfangtoutiao',
    'Bilibili' = "bilibili",
}



export class QuDaoApiClass {

    /* 广点通 */
    async guangdiantong(mode: "jihuo" | "zhuche" | "fuzhi" | 'baomin' | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {

        await qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '广点通' })
        try {
            const url: string = `https://api.e.qq.com/v1.1/user_actions/add?access_token=${result.access_token}&timestamp=${Math.floor(Date.now() / 1000)}&nonce=${Date.now()}`
            let action_type: string
            switch (mode) {
                case 'jihuo':
                    action_type = 'ACTIVATE_APP'
                    break
                case 'zhuche':
                    action_type = 'REGISTER'
                    break
                default:
                    return;
            }
            const jihuo = await axios.post(url,
                {
                    'account_id': result.account_id,
                    "user_action_set_id": result.user_action_set_id,
                    "actions": [
                        {
                            "action_time": Math.floor(Date.now() / 1000),
                            "user_id": {
                                "hash_imei": result.muid,
                                "oaid": result.oaid
                            },
                            "action_type": action_type,
                            "trace": {
                                "click_id": result.click_id
                            }
                        }
                    ]
                }
            )
            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }


    /* 百度 */
    async baidu(mode: "jihuo" | "zhuche" | "fuzhi" | 'baomin' | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {
        await qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '百度' })
        try {
            let a_type: string
            switch (mode) {
                case 'jihuo':
                    a_type = 'activate'
                    break
                case 'zhuche':
                    a_type = 'register'
                    break
                default:
                    return;
            }

            let url: string = unescape(result.callback)
            url = url.replace('{{ATYPE}}', a_type)
            url = url.replace('{{AVALUE}}', '0');

            const md5 = crypto.createHash('md5');
            const sign = md5.update(url + (result as any).akey).digest('hex')
            url += "&sign=" + sign;

            const jihuo = await axios.get(url)

            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }

    /* Bilibili */
    async bilibili(mode: "jihuo" | "zhuche" | "fuzhi" | 'baomin' | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {
        await qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: 'Bilibili' })
        try {
            let conv_type: string
            switch (mode) {
                case 'jihuo':
                    conv_type = 'APP_FIRST_ACTIVE'
                    break
                case 'zhuche':
                    conv_type = 'USER_REGISTER'
                    break
                case 'fuzhi':
                    conv_type = 'ACTION_VALID'
                    break
                default:
                    return;
            }

            const jihuo = await axios.get(unescape(result.callback), {
                params: {
                    conv_type,
                    conv_time: Math.floor(Date.now() / 1000)
                }
            })
            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }


    /* 搜狗 */
    async shougou(mode: "jihuo" | "zhuche" | "fuzhi" | 'baomin' | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {
        await qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '搜狗' })
        try {
            let a_type: string
            switch (mode) {
                case 'jihuo':
                    a_type = '105'
                    break
                case 'zhuche':
                    a_type = '101'
                    break
                default:
                    return;
            }

            let url: string = unescape(result.callback)
            url = url.replace('{{ATYPE}}', a_type)
            const jihuo = await axios.get(url)
            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }


    /* 趣头条 */
    async qutoutiao(mode: "jihuo" | "zhuche" | "fuzhi" | 'baomin' | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {
        await qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '趣头条' })
        try {
            let op2: string
            switch (mode) {
                case 'jihuo':
                    op2 = '0'
                    break
                case 'zhuche':
                    op2 = '1'
                    break
                case 'fuzhi':
                    op2 = '7'
                    break
                default:
                    return;
            }

            const jihuo = await axios.get(unescape(result.callback), {
                params: {
                    op2,
                    opt_active_time: Math.floor(Date.now() / 1000)
                }
            })
            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }

    /* UC */
    async uc(mode: "jihuo" | "zhuche" | "fuzhi" | 'baomin' | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {
        await qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: 'UC' })
        try {
            let type: number
            switch (mode) {
                case 'jihuo':
                    type = 1
                    break
                case 'zhuche':
                    type = 27
                    break
                default:
                    return;
            }

            const jihuo = await axios.get(unescape(result.callback), {
                params: {
                    type,
                    event_time: Date.now()
                }
            })
            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }


    /*  聚流宝*/
    async juliubao(mode: "jihuo" | "zhuche" | "fuzhi" | 'baomin' | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {

        await qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '聚流宝' })

        try {
            const url: string = 'https://naga.convergemob.com/naga/dsp/transform'
            let event_type: number
            switch (mode) {
                // case 'jihuo':
                //     event_type = 0
                //     break
                case 'zhuche':
                    event_type = 0
                    break
                case 'fuzhi':
                    event_type = 19
                    break
                default:
                    return;
            }
            const jihuo = await axios.get(url, {
                params: {
                    clickid: result.callback,
                    event_type,
                    conv_time: Math.floor(Date.now() / 1000)
                }
            })
            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }


    /* 新浪*/
    async xinlang(mode: "jihuo" | "zhuche" | "fuzhi" | 'baomin' | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {
        await qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '新浪' })
        try {
            const url: string = `http://cnv.ad.sina.com.cn/track?cnv_id=${(result as any).cnv_id}&click_id=${result.callback}&bhv_time=${Math.floor(Date.now() / 1000)}`
            const token = (result as any).token
            if (mode == 'jihuo') {
                let method = crypto.createHmac('sha1', token)
                method.setEncoding('base64')
                method.write(url)
                method.end()
                let sign = method.read()
                setTimeout(async () => {
                    const jihuo = await axios.get(url, {
                        params: {
                            sign: sign.replace("/", '_'),
                        }
                    })
                    console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
                    return jihuo.data
                }, 200);
            }
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }


    /*  有道*/
    async youdao(mode: "jihuo" | "zhuche" | "fuzhi" | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {
        qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '有道' }).then()
        try {
            const url: string = 'http://conv.youdao.com/api/track'
            let conv_action: string
            switch (mode) {
                case 'jihuo':
                    conv_action = 'android_activate'
                    break
                case 'zhuche':
                    conv_action = 'android_register'
                    break
                default:
                    return;
            }

            const jihuo = await axios.get(url, {
                params: {
                    conv_ext: result.callback,
                    conv_action
                }
            })
            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }


    /*  快手*/
    async kuaishou(mode: "jihuo" | "zhuche" | "fuzhi" | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {
        qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '快手' }).then()
        try {

            let event_type: string
            switch (mode) {
                case 'zhuche':
                    event_type = '1';
                    break;
                case 'fuzhi':
                    event_type = '10';
                    break;
                default:
                    return;
            }

            const jihuo = await axios.get(unescape(result.callback), {
                params: {
                    event_time: Math.floor(Date.now() / 1000),
                    event_type
                }
            })

            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }


    /* 东方头条 */
    async dongfangtoutiao(mode: "jihuo" | "zhuche" | "fuzhi" | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {
        qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '东方头条' }).then()
        try {

            let event_type: string
            switch (mode) {
                case 'jihuo':
                    event_type = '0'
                    break
                case 'zhuche':
                    event_type = '3'
                    break
                default:
                    return;
            }

            let url: string = unescape(result.callback)
            url = url.replace('__CONV_TIME__', Math.floor(Date.now() / 1000).toString())
            url = url.replace('__EVENT_TYPE__', event_type)

            const jihuo = await axios.get(url)

            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });

            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }


    /* 喜马拉雅 */
    async xmly(mode: "jihuo" | "zhuche" | "fuzhi" | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {
        qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '喜马拉雅' }).then()
        try {
            let type: string
            switch (mode) {
                case 'jihuo':
                    type = 'act'
                    break
                case 'zhuche':
                    type = 'register'
                    break
                default:
                    return;
            }
            const jihuo = await axios.get(unescape(result.callback), {
                params: {
                    type
                }
            })

            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }


    /* 知乎 */
    async zhihu(mode: "jihuo" | "zhuche" | "fuzhi" | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {

        qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '知乎' }).then()

        try {
            let event: string
            switch (mode) {
                case 'jihuo':
                    event = 'install'
                    break
                default:
                    return;
            }

            const jihuo = await axios.get(unescape(result.callback), {
                params: {
                    ts: Math.floor(Date.now() / 1000),
                    event
                }
            })

            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });

            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }


    /* 抖音 */
    async douyin(mode: "jihuo" | "zhuche" | "fuzhi" | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {

        qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '抖音' }).then()
        try {
            let event_type: number
            switch (mode) {
                case 'jihuo':
                    event_type = 0
                    break
                case 'zhuche':
                    event_type = 1
                    break
                default:
                    return;
            }
            const jihuo = await axios.get(unescape(result.callback),
                {
                    params: {
                        conv_time: Math.floor(Date.now() / 1000),
                        event_type
                    }
                }
            )
            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }


    /* 刷宝 */
    async shubao(mode: "jihuo" | "zhuche" | "fuzhi" | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {

        qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '刷宝' }).then()
        try {

            const jihuo = await axios.get(unescape(result.callback))
            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }


    /* 乐游 */
    async leyou(mode: "jihuo" | "zhuche" | "fuzhi" | 'tiaozuan', result: IdfaModel, qudaotj: ModelType<QudaotjModel>) {
        qudaotj.create({ mark: result.ditch, createTime: Math.floor(Date.now() / 1000), type: mode, channel: '乐游' }).then()
        try {
            let type: string
            switch (mode) {
                case 'jihuo':
                    type = '0'
                    break
                case 'zhuche':
                    type = '1'
                    break
                default:
                    return;
            }
            const jihuo = await axios.get(unescape(result.callback), { params: { type } })
            console.log({ msg: mode, mark: result.ditch, ...jihuo.data });
            return jihuo.data
        } catch (error) {
            if (error.response) {
                console.log(error.response.data);
            } else {
                throw error
            }
        }
    }

}
